import inspect
from logger import logger

# 组件注册表
_component_registry = {}

def component(name=None):
    def decorator(cls):
        logger.debug(f"Registering component: {name or cls.__name__.lower()}")
        component_name = name or cls.__name__.lower()
        _component_registry[component_name] = cls
        return cls
    return decorator

# 应用上下文
class ApplicationContext:
    def __init__(self):
        self.instances = {}
    
    def initialize(self):
        """初始化所有组件"""
        logger.debug("Initializing components...")
        for name, cls in _component_registry.items():
            logger.debug(f"Initializing component: {name}")
            self.instances[name] = cls()
    
    def set(self, name, instance):
        self.instances[name] = instance

    def get(self, name):
        return self.instances.get(name)

context = ApplicationContext()
